package com.web.servlet;


import com.web.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import com.web.JBDCUtils;

@WebServlet("/searchStudents")
public class SearchStudentsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求编码
        req.setCharacterEncoding("UTF-8");
        //设置响应编码
        resp.setCharacterEncoding("UTF-8");
        PrintWriter out = resp.getWriter();
        String keyword = req.getParameter("keyword");
        if (keyword != null && !keyword.isEmpty()) {
            List<Student> list = JBDCUtils.query(Student.class,"select*from student where name like ?" ,"%"+keyword+"%");
            for (Student student : list) {
                out.println(student.toString());
            }
        } else {
            out.println("请提供搜索关键字");
        }
    }
}